var systemInterfaceParam=null;
$(document).ready(function(){
	var urlObj = getParamobjFromUrl();
	var fieldId = urlObj.fieldId;
	var fieldName = urlObj.fieldName? decodeURI(urlObj.fieldName):"";
	var startTime = urlObj.startTime? urlObj.startTime:"";
	var endTime = urlObj.endTime? urlObj.endTime:"";
	var typeCode = urlObj.type && urlObj.type == "fail"? "1":"";
	$('#txtBeginTime').datetimebox('setValue', startTime.replace("T", " "));
	$('#txtEndTime').datetimebox('setValue', endTime.replace("T", " "));
	$(".button_con a" ).removeClass("l-btn-plain");
	//初始化执行时间
	initailDate();
	$('#comboxSearchFieldName').combobox({
		url: BASE_URL_FIELD_SEARCH + '/findAllOfCurrentUserCanManage' + '?v=' + Math.random(),
		valueField: 'id',
	    textField: 'name',
	    editable:false,
	    method:'get',
	    loadFilter: function(data){
	    	var allCheck = {'id':'','name':'全部'};
	    	data.content.unshift(allCheck);
	    	return data.content;
	    },
	    onSelect: function(record){
	    	comboxSearchSystem.combobox({
                disabled: false,
                method:'get',
                queryParams: {fieldId: record.id},
                url: BASE_URL_FIELD_SYSTEM_SEARCH +'/findByFieldOfCurrentUserCanManage' + '?v=' + Math.random(),
                valueField: 'id',
                textField: 'name',
				data:'',
    		    loadFilter: function(data){
    		    	var allCheck = {'id':'','name':'全部'};
    		    	data.content.unshift(allCheck);
    		    	return data.content;
    		    },
    		    onLoadSuccess:function(){
    		    	comboxSearchSystem.combobox('setValue','');
    		    }
            }).load();
	    },
	    onLoadSuccess:function(){
	    	
	    	if(fieldId)
	    	{
	    		$('#comboxSearchFieldName').combobox('select',fieldId);
	    		//$("#datagrid").datagrid("reload");
	    	}
	    	else
	    	{
	    		$('#comboxSearchFieldName').combobox('setValue','');
	    	}
	    }		
	});
	
	var comboxSearchSystem = $('#comboxSearchSystem').combobox({
        disabled: true,
        valueField: 'id',
        textField: 'name',
        editable:false,
		data:[{id:'',name:'全部','selected':'true'}]
    });
	
	$('#comboxSearchImplementation').combobox({
		editable: false,
		url: BASE_URL_DATA_DICTIONARY_VALUE_SEARCH +'/findByTypeCode?typeCode='+ DDTC_KETTLE_JOB_EXEC_STATUS + '&v=' + Math.random(),
		valueField: 'valueCode',
		textField: 'valueName',
	    editable:false,
	    method:'get',
	    loadFilter: function(data){
	    	var allCheck = {'valueCode':'','valueName':'全部'};
		    	data.unshift(allCheck);
	    	return data;
		},
	    onLoadSuccess:function(){
	    	if(typeCode)
	    	{
	    		$('#comboxSearchImplementation').combobox('select',typeCode);
	    		typeCode = null;
	    		//$("#datagrid").datagrid("reload");
	    	}
	    	else
	    	{
	    		$('#comboxSearchImplementation').combobox('setValue','');
	    	}
	    }			
	});
	
	$("#datagrid").datagrid({
			//url :BASE_URL_HISTORY_INTERFACE_SEARCH +'/findByExample' + '?v=' + Math.random(),
			method:'get',
			autoRowHeight:true,//自动行高
			singleSelect: true,//单选
			pagination:true,
			nowrap:false,
			striped:true,
			pageSize:20,
			pageList:[10,20,50,100],
			columns:[[
						{field:'',width:10,checkbox:true},
						{field:'fieldName',title:'领域',width:60},
						{field:'fieldSystemName',title:'系统',width:60},
						{field:'interfaceName',title:'接口名称',width:120},
						{field:'scheduleName',title:'调度名',width:100},
						{field:'runResultForDisplay',title:'执行情况',width:70},
						{field:'slaveServerId',title:'节点编号',width:50},
						{field:'runBeginTime',title:'执行开始时间',width:100},
						{field:'runEndTime',title:'执行结束时间',width:100},
						{field:'runTimes',title:'执行时长(秒)',width:80},
						{field:'inputQTY',title:'输入量',width:50},
						{field:'recordQTY',title:'输出量',width:50},
						{field:'operation',title:'操作',width:80,
							formatter: function(value,row,index){
								//alert("id:"+row.id);
								return '<a href="#" class="default_btn" onclick="showLog(' + index+ ')">查看日志</a>';		
							}}
			      ]],
	        onSelect:function(rowIndex,rowData){
			},
			loadFilter:function(data){
				return {
					total: data.totalElements,
					rows: data.content
				};
			},
			onBeforeLoad:function(param){
				param.page -= 1;
				param.size = param.rows;
				delete param.rows;
				
				
				param.sort = 'createTime,desc';
				var fieldName1 = $('#comboxSearchFieldName').combobox('getText');
				if(fieldName1 !="全部"){
					param.fieldName = fieldName1;
				}
				
				if(fieldName){
					param.fieldName = fieldName;
					fieldName = null;
				}
				var fieldSystemName = $('#comboxSearchSystem').combobox('getText');
				if(fieldSystemName !="全部"){
					param.fieldSystemName = fieldSystemName;
				}
				var result = $('#comboxSearchImplementation').combobox('getValue');
				if(result !="全部"){
					param.result = result;
				}
				if(typeCode)
				{
					param.result = typeCode;
					//typeCode = null;
				}
				var interfaceName = $('#txtSearchInterfaceName').val();
				if(interfaceName != ""){
					param.interfaceName = interfaceName;
				}
				
				var beginOfCreateTime = $('#txtBeginTime').datetimebox('getValue');
				if(beginOfCreateTime !=""){
					param.beginOfCreateTime = beginOfCreateTime;
				}
				
				var endOfCreateTime = $('#txtEndTime').datetimebox('getValue');
				if(endOfCreateTime !=""){
					param.endOfCreateTime = endOfCreateTime;
				}
				
				var scheduleName1 =$('#txtSearchScheduleName').val();
				if(scheduleName1 != ""){
					param.scheduleName1 = scheduleName1;
				}
				systemInterfaceParam = param;
				var quantity = $('#quantity').combobox('getText');
				if(quantity != "全部"){
					param.quantity = quantity;
				}
				var inputQTY = $('#inputQTY').combobox('getText');
				if(inputQTY != "全部"){
					param.inputQTY = inputQTY;
				}
			}
		});
		
	$("#datagrid").datagrid('getPager').pagination({   
		displayMsg:'当前显示从 [{from}] 到 [{to}] 共[{total}]条记录'
	}); 
	
	//查看日志
	$('#tips_log').window({
		width:550,
		modal:true,
		minimizable:false,
		maximizable:false,
		collapsible:false
	});
	$('#tips_log').window('close');  //关闭窗口
	
	
	
		$("#tips_log").siblings(".panel-header").children(".panel-tool").children(".panel-tool-close").click(function(e)	
	{
		//$("#datagrid").datagrid("reload",BASE_URL_HISTORY_INTERFACE_SEARCH +'/findByExample' + '?v=' + Math.random());
     });	
	
	
});

function initailDate() {

	var curr_time = new Date();
	var year = curr_time.getFullYear();
	var month = curr_time.getMonth() + 1;
	var day = curr_time.getDate();
	var hours = curr_time.getHours();
	var minutes = curr_time.getMinutes();
	var seconds = curr_time.getSeconds();
	if (month < 10) {
		month = '0' + month;
	}
	var dayStart = day -7;
	if (dayStart < 10) {
		dayStart = '0' + dayStart;
	}
	
	if (day < 10) {
		day = '0' + day;
	}
	if (hours < 10) {
		hours = '0' + hours;
	}
	if (minutes < 10) {
		minutes = '0' + minutes;
	}
	if (seconds < 10) {
		seconds = '0' + seconds;
	}
	var start = year + '-' + month + '-' + dayStart + ' 00:00:00';
	var end = year + '-' + month + '-' + day + ' 23:59:59';

	$("#txtBeginTime").datetimebox("setValue", start);
	$("#txtEndTime").datetimebox("setValue", end);
}
	//查看日志操作
	function showLog(index)
	{
		$('#datagrid').datagrid('selectRow',index);
		var row = $('#datagrid').datagrid('getSelected');
				
		if(row == null){
			$.messager.alert("警告","<p>请选中要查看的记录!</p>","warning");
		}else{
			$.ajax({
				url:BASE_URL_HISTORY_INTERFACE_SEARCH+ '/logs' + "?id=" + row.id + '&v=' + Math.random(),
				dataType:'json',
				type:'get',
				contentType:"application/json",
				success:function(data){
					if(data.logField !== null)
					{
						var logArr = [];
						var html = '';
						var logField = data.logField;
						logArr = logField.split("&lt;br&gt;");
						for(var i = 0, len = logArr.length; i < len; i++)
						{
							if(i % 2 == 0)
							{
								if(logArr[i] === "null")
								{
									html += '<p class="even"><span>' + logArr[i] + '</span></p>';
								}else
								{
									html += '<p class="even"><strong>' + logArr[i].split(" - ")[0] + '</strong><br /><span>' +logArr[i].split(" - ")[1] + " - " + logArr[i].split(" - ")[2] + '</span></p>';
								}
							}else
							{
								if(logArr[i] === "null")
								{
									html += '<p class="odd"><span>' + logArr[i] + '</span></p>';
								}else
								{
									html += '<p class="odd"><strong>' + logArr[i].split(" - ")[0] + '</strong><br /><span>' + logArr[i].split(" - ")[1] + " - " +  logArr[i].split(" - ")[2] + '</span></p>';
								}
							}
						}
						//fieldSystemName.replace("\r\n", "<br />");			
						$("#logTxt").html(html);
						$('#tips_log').window('open');
					}else
					{
						$.messager.alert("警告","<p>日志记录为空！</p>","warning",function(e)
						{
								//$("#datagrid").datagrid("reload", BASE_URL_HISTORY_INTERFACE_SEARCH +'/findByExample' + '?v=' + Math.random());
						});
						
					}
				}
			});
			
			 
		}		
	}

	
	
	//导出日志操作
	function import_link()
	{
	}
	
	//导出报表操作
	function report()
	{
		$('#datagrid').datagrid({
			url:BASE_URL_HISTORY_INTERFACE_SEARCH +'/findByExample' + '?v=' + Math.random()
		});
		var param = JSON.stringify(systemInterfaceParam);
		location.href= BASE_URL_EXPORTDATA_FILE + '/historyInterfaceInfoExport?param='+param;
	}
	
	//查询
	function onSearch(){
		var opts = $("#datagrid").datagrid("options");
	    opts.url = BASE_URL_HISTORY_INTERFACE_SEARCH +'/findByExample' + '?v=' + Math.random();
		dataRetrieval('#txtBeginTime','#txtEndTime');
	}